$.borrow = function(lHTTP,$scope,lPop){
    var isChanger = 0;
    var isBuy = 0;//1表示购线，0表示借充电宝
    var lineType = 0;//1表示ip5  2表示ip4  3表示android
    var device = "";//设备号
    var buyLineMoney = 1000;//充值购线金额（服务器获取）
    var borrowCDBMoney = 10000;//租借充电宝金额（服务器获取）
    var t2 = undefined;//定义定时查询用户事件
    var t = undefined;//定义定时查出充电宝事件
    var requestTime = 0;//查询超时事件
    var taskId = 0;//用于查询出充电宝任务Id
    var isFirstBorrowSucc = 0;//用于判断用户是否第一次租借成功
    var presentLineBuyCnt = 0;//赠送免费购线机会
    var isEnough = -1; //用户是否有足够金额租借充电宝或购线
    var shortMoney = 0; //如果不够金额租借或者购线，相差的金额数
    var popMessage = "";
    var popTitle = "";
    var isReturn = 0;
    var chargeType = 0;

    //查询用户信息
    $.updateUserInfo = function(){

        lHTTP.jsonp("/UserGet?static=1")
            .success(function(json,status){
                //判断用户是否购线余额不足或者租借余额不足查询用户信息
                    if(isChanger == 1){
                        if(parseFloat(json.user.money) > userMoney){
                            //停止循环访问
                            clearInterval(t2);
                            //如果之前的操作为租借充电宝就调用出充电宝方法
                            if(isBuy == 0){
                                $.taskAdd();
                            }
                            else{
                                $.taskAddBuyLine();
                            }
                            isChanger = 0;
                        }
                    }
                    if(json.user.mobile != "" && json.user.mobile != null){
                        $('.pick-up-link').attr('href','#/pickUp');
                        $('.borrow .download-link').attr('href','http://downloads.imlaidian.com');
                    }

                    //储存用户金额
                    userMoney = parseFloat(json.user.money);
                    //储存用户赠送金额
                    presentMoney = parseFloat(json.user.presentMoney);
                    //储存押金
                    preMoney = parseFloat(json.user.preMoney);

                    presentLineBuyCnt = json.user.cnt.presentLineBuyCnt;

                    //$('.borrow .userHeadImg').attr('src',json.user.headImage);
                    $('.borrow .nickName').text(json.user.nickName);
                    $('.borrow .money').text(userMoney.toFixed(1)+"元");
                    $('.borrow .preMoney').text(preMoney.toFixed(1)+"元");

                    var isRetunBuy = getQueryString(window.location,"result");
                    if(isRetunBuy != "" && isRetunBuy != null){
                        if(isRetunBuy == "success" && isReturn == 0){
                            isBuy = getCookie("isBuy");
                            device = getCookie("device");
                            lineType = getCookie("lineType");
                            if(isBuy == 0){
                                $.taskAdd();
                            }
                            else{
                                $.taskAddBuyLine();
                            }
                            isReturn = 1;
                        }
                    }

            });
        //$.getJSON(requestHost+"UserGet?callback=?&access_token="+access_token,function(json){
        //    //判断用户是否购线余额不足或者租借余额不足查询用户信息
        //    if(isChanger == 1){
        //        if(parseFloat(json.user.money) > userMoney){
        //            //停止循环访问
        //            clearInterval(t2);
        //            //如果之前的操作为租借充电宝就调用出充电宝方法
        //            if(isBuy == 0){
        //                $.taskAdd();
        //            }
        //            else{
        //                $.taskAddBuyLine();
        //            }
        //            isChanger = 0;
        //        }
        //    }
        //    if(json.user.mobile != "" && json.user.mobile != null){
        //        $('.pick-up-link').attr('href','#pickUp');
        //    }
        //
        //    //储存用户金额
        //    userMoney = parseFloat(json.user.money);
        //    //储存用户赠送金额
        //    presentMoney = parseFloat(json.user.presentMoney);
        //    //储存押金
        //    preMoney = parseFloat(json.user.preMoney);
        //
        //    $('.borrow .userHeadImg').attr('src',json.user.headImage);
        //    $('.borrow .nickName').text(json.user.nickName);
        //    $('.borrow .money').text(userMoney.toFixed(1)+"元");
        //    $('.borrow .preMoney').text(preMoney.toFixed(1)+"元");
        //
        //
        //});
    }

    //查询购买记录
    $.selectBuy = function(){
        if(requestTime >= 17){
            clearInterval(t2);
            clearInterval(t);
            $.popUpMessage(4, "网络超时", "", "知道了", null, null, null, 1);
            requestTime=0;
            return false;
        }
        requestTime = requestTime+1;
        //$.getJSON(requestHost+"taskGet?callback=?&access_token="+access_token+"&taskId="+taskId+lastParameter,function(json){
            lHTTP.jsonp("/taskGet?taskId="+taskId)
                .success(function(json,status){
            if(json.taskModel.status == 1){
                $scope.showPopTips = false;
                clearInterval(t);
                //判断用户赠送金额是否大于购线金额
                if(presentLineBuyCnt > 0 && isBuy == 0){
                    if(device.split('0007550')[1].charAt(0) == "2"){
                        lPop.popSet(1 , $scope ,"该机不具售线功能" ,"请到服务员处领取数据线" , "images/prompt-icon.png" , undefined,undefined ,"确定", undefined,undefined);
                        return false;
                    }
                    isFirstBorrowSucc = 1;
                    $('.borrow .buy-line-model-2').show();
                    $('.borrow .buy-line-model-2 .title').css('line-height','1.2em');
                    $('.borrow .buy-line-model-2 .title-div2').hide();
                    $('.borrow .buy-line-model-2 .title-div1').show();
                    $('.borrow .buy-line-model-2 .title2').show();
                    if(chargeType == 2){
                        $('.borrow .buy-line-model-2 .title .font2').hide();
                    }
                    else{
                        $('.borrow .buy-line-model-2 .title2 .font2').show();
                    }
                    $('.borrow .buy-line-bg').show();
                }
                else if(isBuy == 0){
                    //$scope.showPopTips = false;
                    lPop.popSet(2 , $scope ,"租借成功" ,undefined , "images/right-icon.png" , undefined,undefined , "请取走机器吐出的充电宝" , undefined,3000);

                }
                else{
                    //$scope.showPopTips = false;
                    lPop.popSet(2 , $scope ,"购买成功" ,undefined , "images/right-icon.png" , undefined,undefined , "请取走机器吐出的数据线" , undefined,3000);
                }
                requestTime=0;
            }
            else if(json.taskModel.status <= 0){
                //$scope.showPopTips = false;
                clearInterval(t);
                lPop.popSet(2 , $scope ,json.taskModel.popMessage ,undefined , "images/wrong-icon.png" , undefined,undefined , "知道了" , undefined,3000);
                requestTime=0;
            }
            //else if(json.taskModel.status == -1){
            //    //$scope.showPopTips = false;
            //    clearInterval(t);
            //    //$.popUpMessage(1, "没有充电器/充电线了", "", "知道了", null, null, null, 1);
            //    if(isBuy == 0) {
            //        //$.popUpMessage(4, "该设备无充电宝了", "", "知道了", null, null, null, 1);
            //        lPop.popSet(1, $scope ,json.taskModel.popMessage ,"是否去附近来电？" , "images/wrong-icon.png" , "好的",function(){location.href="#/nearLaidian";} , "下次吧" , undefined,undefined);
            //    }
            //    else{
            //        //$.popUpMessage(4, "该设备无数据线", "", "知道了", null, null, null, 1);
            //        lPop.popSet(1, $scope ,json.taskModel.popMessage ,"是否去附近来电？" , "images/wrong-icon.png" , "好的",function(){location.href="#/nearLaidian";}, "下次吧" , undefined,undefined);
            //    }
            //    requestTime=0;
            //}
            //else if(json.taskModel.status == -2 && json.taskModel.status == -3){
            //    //$scope.showPopTips = false;
            //    //$scope.poptips = false;
            //    clearInterval(t);
            //    //$.popUpMessage(1, "终端发生错误", "", "知道了", null, null, null, 1);
            //    lPop.popSet(2 , $scope ,json.taskModel.popMessage ,json.taskModel.popMessage , "images/wrong-icon.png" , undefined,undefined , "知道了" , undefined,3000);
            //    requestTime=0;
            //}
            //else if(json.taskModel.status == -5){
            //    //$scope.showPopTips = false;
            //    //$scope.poptips = false;
            //    clearInterval(t);
            //    //$.popUpMessage(1, "您借取的充电宝未提取", "", "知道了", null, null, null, 1);
            //    lPop.popSet(2 , $scope ,json.taskModel.popMessage ,undefined , "images/wrong-icon.png" , undefined,undefined , "知道了" , undefined,3000);
            //    requestTime=0;
            //}
        })
    }

    //第一次登陆查询一次用户信息
    if(access_token != "" && access_token != null){
        $.updateUserInfo();
    }

    $('.open-button').click(function(){
        $('.borrow .icon-bg').show();
        $.updateUserInfo();
    })

    $('.close-button').click(function(){
        $('.borrow .icon-bg').hide();
    })

    //拉起支付
    var prepaid = function(){
        //$('#charge-button').click(function(){
        $scope.showPopTips = false;
            var amount = 0;
            if(shortMoney != 0){
                amount = shortMoney;
            }
            else if(isBuy == 0){
                amount = borrowCDBMoney;
            }
            else{
                amount = buyLineMoney;
            }

            //将用户购线与租充电宝的标志位记录在cookie中
            setCookie("isBuy", isBuy);
            setCookie("lineType", lineType);

            lPop.popSet(2 , $scope ,"正在响应中" ,undefined , "images/loding-icon.png" , undefined,undefined , "正在处理支付中" , undefined,undefined);
            //$.getJSON(requestHost+"chargeCreate?callback=?&access_token="+access_token+"&channel=wx_pub&amount="+amount,function(json){
                lHTTP.jsonp("/chargeCreate?channel=alipay_wap&amount="+amount+"&alipayWapSuccUrl="+encodeURIComponent("http://szt.imlaidian.com/app/#/"))
                    .success(function(json,status){
                pingpp.createPayment(json, function(result, err) {
                    if(result == "success"){
                        isChanger = 1;
                        //循环访问用户信息看充值是否到账
                        t2 = window.setInterval("$.updateUserInfo()",2000);
                        lPop.popSet(2 , $scope ,"正在响应中" ,undefined , "images/loding-icon.png" , undefined,undefined , "请查看机器是否吐出充电宝" , undefined,undefined);
                    }
                    else if (result == "fail") {
                        //$.prompt('',1,'','请重新扫描',1,1,6000);
                        //$.getJSON("/"+server+"/taskAddError",function(json){
                        //});
                        //alert('ok')
                        //$scope.showPopTips = false;
                        lPop.popSet(2 , $scope ,"支付失败" ,undefined , "images/loding-icon.png" , undefined,undefined , "" , undefined,1);
                    }
                    else if (result == "cancel") {
                        // 微信公众账号支付取消支付
                        //alert('cancel')
                        //alert($scope)
                        //$scope.showPopTips = false;
                        lPop.popSet(1 , $scope ,"取消支付" ,"你已经取消支付" , "images/loding-icon.png" , undefined,undefined , "" , undefined,1);
                    }
                });
            })
            //$.popUpIsHide();
        //})
    }



    //购买充电线
    $.taskAddBuyLine = function(){

        //$.getJSON(requestHost+"taskAddBuyLine?access_token="+access_token+"&terminal="+device+"&lineType="+lineType+lastParameter,function(json){
            lHTTP.jsonp("/taskAddBuyLine?terminal="+device+"&lineType="+lineType)
                .success(function(json,status){
            if(json.result == 1){
                taskId = json.taskId;
                //$('.scan').attr("disabled","true");
                //getUserType = 0;
                //$.popUpMessage(3, "正在响应中", "", "请查看机器是否吐出数据线", null, null, null, 1);
                t = window.setInterval("$.selectBuy()",2000);
                lPop.popSet(2 , $scope ,"正在响应中" ,undefined , "images/loding-icon.png" , undefined,undefined , "请查看机器是否吐出数据线" , undefined,undefined);
            }
            else{
                lPop.popSet(2 , $scope ,json.msg ,undefined , "images/wrong-icon.png" , undefined,undefined , "知道了" , undefined,3000);
            }
        });
    }

    //租借充电宝
    $.taskAdd = function(){
        //$.getJSON(requestHost+"taskAdd?callback=?&access_token="+access_token+"&terminal="+device+lastParameter,function(json){
            lHTTP.jsonp("/taskAdd?terminal="+device)
                .success(function(json,status){
            //alert(json.result);
            // var tokenJSON = eval('('+json+')');
            if(json.result == 1){
                taskId = json.taskId;
                //$('.scan').attr("disabled","true");
                //getUserType = 0;
                //$.popUpMessage(3, "正在响应中", "", "请查看机器是否吐出充电宝", null, null, null, 1);
                t = window.setInterval("$.selectBuy()",2000);
                lPop.popSet(2 , $scope ,"正在响应中" ,undefined , "images/loding-icon.png" , undefined,undefined , "请查看机器是否吐出充电宝" , undefined,undefined);
            }
            else{
                //$.popUpMessage(4, json.msg, "", "知道了", null, null, null, 1);
                lPop.popSet(2 , $scope ,json.msg ,undefined , "images/wrong-icon.png" , undefined,undefined , "知道了" , undefined,3000);
            }
        });
    }


    $('.go-pay-button').click(function(){
        $('.device-pay').hide();
        $('.buy-line-bg').hide();
        if (userMoney >= borrowCDBMoney/100) {
            $.taskAdd();
        }
        else{
            //alert("支付页面")
            $scope.showPopTips = false;
            //$.popUpMessage(2, "请充值", "借充电宝需要100元押金", "取消", null, "充值", "charge-button", 1)
            lPop.popSet(1, $scope ,"余额不足" ,popMessage , "images/prompt-icon.png" , "支付押金" ,prepaid , "取消支付" ,undefined ,undefined);
            //调用绑定充值事件
            //$.prepaid();
        }
    })

    var bindFirstBorrowBuyLine = function(){
        //$('#firstBorrowBuy').click(function(){
        $.taskAddBuyLine();
        isFirstBorrowSucc = 0;
        $.popUpIsHide();
        //})
    }

    $.scan = function(scanDevice){
        //更新用户信息
        $.updateUserInfo();
        device = getQueryString(scanDevice,"qrCode");
        //将设备号储存在cookie中
        setCookie("device", device);

        //判断购线或者租借
        var checkType = 0;
        if(isBuy == 0){
            checkType = 1;
        }
        else{
            checkType = 2;
        }

        //获取设备是否有充电宝
        lHTTP.jsonp("/deviceCheckLineAndCDB?terminal="+device+"&type="+checkType)
            .success(function(json,status){
                //查询是否还有充电宝
                    buyLineMoney = parseFloat(json.deviceCharge.buyLinePay)*100;
                    borrowCDBMoney = parseFloat(json.deviceCharge.borrowCDBMoney)*100;
                    isEnough = parseFloat(json.isEnough);
                    shortMoney = parseFloat(json.shortMoney);
                    popMessage = json.popMessage;
                    popTitle = json.popTitle;
                    chargeType = json.deviceCharge.chargeType;
                    if(json.result == 1){
                        //租借充电宝
                        if(isBuy == 0) {
                            //alert(json.deviceCharge.chargeType)
                            if(chargeType == 2){
                                $('.device-pay .device-terminal').text(json.terminal);
                                $('.device-pay .shop-name').text(json.shop.name);
                                $('.device-pay .gratis-time').text(json.deviceCharge.gratisTimeStr);
                                $('.device-pay .hour-pay').text(json.deviceCharge.hourPayStr);
                                $('.device-pay .max-pay').text(json.deviceCharge.dayMaxPayStr);
                                $('.device-pay .vip-gratis-time').text(json.deviceCharge.vipGratisTimeStr);
                                $('.device-pay .vip-hour-pay').text(json.deviceCharge.vipHourPayStr);
                                $('.device-pay .vip-max-pay').text(json.deviceCharge.vipDayMaxPayStr);
                                $('.device-pay .annotation').text(json.deviceCharge.annotation);
                                $('.device-pay').show();
                                $('.buy-line-bg').show();
                            }
                            else{
                                if (isEnough == 1) {
                                    $.taskAdd();
                                }
                                else{
                                    lPop.popSet(1, $scope ,popTitle ,popMessage , "images/prompt-icon.png" , "支付押金" ,prepaid , "取消支付" ,undefined ,undefined);
                                }
                            }
                        }
                        //购买数据线
                        else{
                            if(device.split('0007550')[1].charAt(0) == "2"){
                                lPop.popSet(1 , $scope ,"该机不具售线功能" ,"请到服务员处领取数据线" , "images/prompt-icon.png" , undefined,undefined ,"确定", undefined,undefined);
                                return false;
                            }
                            if(isEnough == 1){
                                lPop.popSet(1, $scope ,popTitle ,popMessage , "images/prompt-icon.png" ,  "确定",bindFirstBorrowBuyLine, "取消",undefined,undefined);
                            }
                            else{
                                lPop.popSet(1, $scope ,popTitle ,popMessage , "images/prompt-icon.png" ,  "充值",prepaid, "取消",undefined,undefined);
                            }
                        }
                    }
                    else{
                        if(isBuy == 0) {
                            lPop.popSet(1 , $scope ,"充电宝不足" ,"是否去附近来电？"  , "images/wrong-icon.png" , "好的",function(){location.href="#nearLaidian";} , "下次吧" , undefined,undefined);
                        }
                        else{
                            lPop.popSet(1 , $scope ,"数据线" ,"是否去附近来电？"  , "images/wrong-icon.png" , "好的",function(){location.href="#nearLaidian";}, "下次吧" , undefined,undefined);
                        }
                    }

                })
    }

    $('.borrow .scan-borrow-button').bind('touchstart',function(){
        $(this).css('background','#00C4C3').css('border-color','#FFFFFF').css('color','#FFFFFF');
    })

    //点击借充电宝
    $('.borrow .scan-borrow-button').bind('click',function(){
        //设置为非购买数据线
        isBuy = 0;
        $(this).css('background','#FFFFFF').css('border-color','#000000').css('color','#000000');
        //调用扫描方法
        //$.scan();
    });

    $('.borrow .scan-buy-button').bind('touchstart',function(){
        $(this).css('background','#00C4C3').css('border-color','#FFFFFF').css('color','#FFFFFF');
    })

    //点击购线
    $('.borrow .scan-buy-button').bind('click',function(){
        $(this).css('background','#FFFFFF').css('border-color','#000000').css('color','#000000');
        $('.borrow .buy-line-model').show();
        $('.borrow .buy-line-model .title').css('line-height','4em');
        $('.borrow .buy-line-model .title-div2').show();
        $('.borrow .buy-line-model .title-div1').hide();
        $('.borrow .buy-line-model .title2').hide();
        $('.borrow .buy-line-bg').show();
    })

    $('.borrow .buy-line-bg').click(function(){
        $('.borrow .buy-line-model').hide();
        $(this).hide();
    })

    $('.buy-android-line').click(function(){
        isBuy = 1;
        lineType = 3;
        $('.borrow .buy-line-model').hide();
        $('.borrow .buy-line-bg').hide();
        //if(isFirstBorrowSucc == 0){
        //    $.scan();
        //}
        //else{
        //    //$.popUpMessage(5, "安卓线", "请确认数据线类型", "取消", null, "确定", "firstBorrowBuy", 1);
        //    lPop.popSet(1, $scope ,popTitle ,popMessage, "images/android-line-icon.png" , "确定" ,bindFirstBorrowBuyLine,"取消" , undefined ,undefined);
        //    //$.bindFirstBorrowBuyLine();
        //}
    })

    $('.buy-iphone4-line').click(function(){
        isBuy = 1;
        lineType = 2;
        $('.borrow .buy-line-model').hide();
        $('.borrow .buy-line-bg').hide();
        //if(isFirstBorrowSucc == 0){
        //    $.scan();
        //}
        //else{
        //    //$.popUpMessage(6, "iphone4/4s线", "请确认数据线类型", "取消", null, "确定", "firstBorrowBuy", 1);
        //    lPop.popSet(1, $scope ,popTitle ,popMessage , "images/iphone-line-icon.png" , "确定",bindFirstBorrowBuyLine ,"取消" , undefined ,undefined);
        //    //$.bindFirstBorrowBuyLine();
        //}

    })

    $('.buy-iphone5-line').click(function(){
        isBuy = 1;
        lineType = 1;
        $('.borrow .buy-line-model').hide();
        $('.borrow .buy-line-bg').hide();
        //if(isFirstBorrowSucc == 0){
        //    $.scan();
        //}
        //else{
        //    //$.popUpMessage(6, "iphone5/6线", "请确认数据线类型", "取消", null, "确定", "firstBorrowBuy", 1);
        //    lPop.popSet(1, $scope ,popTitle ,popMessage , "images/iphone-line-icon.png" , "确定",bindFirstBorrowBuyLine ,"取消" , undefined ,undefined);
        //    //$.bindFirstBorrowBuyLine();
        //}

    })

    $.borrowCDBBuyLine = function(){
        $scope.showPopTips = false;
        lHTTP.jsonp("/deviceCheckLineAndCDB?terminal="+device+"&type=2")
            .success(function(json,status){
                isEnough = parseFloat(json.isEnough);
                shortMoney = parseFloat(json.shortMoney);
                popMessage = json.popMessage;
                popTitle = json.popTitle;
                if(device.split('0007550')[1].charAt(0) == "2"){
                    lPop.popSet(1 , $scope ,"该机不具售线功能" ,"请到服务员处领取数据线" , "images/prompt-icon.png" , undefined,undefined ,"确定", undefined,undefined);
                    return false;
                }
                if(isEnough == 1){
                    lPop.popSet(1, $scope ,popTitle ,popMessage , "images/prompt-icon.png" ,  "确定",bindFirstBorrowBuyLine, "取消",undefined,undefined);
                }
                else{
                    lPop.popSet(1, $scope ,popTitle ,popMessage , "images/prompt-icon.png" ,  "充值",prepaid, "取消",undefined,undefined);
                }
            })
    }

    $('.buy-android-line-2').click(function(){
        isBuy = 1;
        lineType = 3;
        $('.borrow .buy-line-model-2').hide();
        $('.borrow .buy-line-bg').hide();
        $.borrowCDBBuyLine();
    })

    $('.buy-iphone4-line-2').click(function(){
        isBuy = 1;
        lineType = 2;
        $('.borrow .buy-line-model-2').hide();
        $('.borrow .buy-line-bg').hide();
        $.borrowCDBBuyLine();
    })

    $('.buy-iphone5-line-2').click(function(){
        isBuy = 1;
        lineType = 1;
        $('.borrow .buy-line-model-2').hide();
        $('.borrow .buy-line-bg').hide();
        $.borrowCDBBuyLine();
    })

}